package com.hcnow.websocket

import com.hcnow.common.config.webSocket.WebSocketMessage
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.messaging.simp.SimpMessagingTemplate
import org.springframework.messaging.simp.user.SimpUserRegistry
import org.springframework.stereotype.Service

@Service
class WebSocketApiService{
    @Autowired
    lateinit var messageTemplate: SimpMessagingTemplate
    @Autowired
    lateinit var userRegistry: SimpUserRegistry

    fun push2Topic(appId:String,projectId:String,channel:String,webSocketMessage: WebSocketMessage){
        val fullTopic:String = "/topic/$appId/$projectId/$channel"
        messageTemplate.convertAndSend(fullTopic,webSocketMessage)
    }

    fun send2User(appId: String, projectId: String, userId: String, channel: String, webSocketMessage: WebSocketMessage) {
        val fullUser:String = "$appId.$projectId.$userId"
        messageTemplate.convertAndSendToUser(userRegistry.getUser(fullUser)?.name, channel,webSocketMessage)
    }

}